#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int Q = 2e5 + 5;

int n, m, q;
int a[305][305];
int bit[105][305][305];
int lowbit(int x) { return x & -x; }
void update(int c, int x, int y, int d) {
  for (int i = x; i <= n; i += lowbit(i)) {
    for (int j = y; j <= m; j += lowbit(j)) {
      bit[c][i][j] += d;
    }
  }
}
int query(int c, int x, int y) {
  int sum = 0;
  for (int i = x; i > 0; i -= lowbit(i)) {
    for (int j = y; j > 0; j -= lowbit(j)) {
      sum += bit[c][i][j];
    }
  }
  return sum;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) rep(j, 1, m) cin >> a[i][j];
  rep(i, 1, n) rep(j, 1, m) update(a[i][j], i, j, 1);

  cin >> q;
  while (q--) {
    int op, x1, y1, x2, y2, c;
    cin >> op;
    if (op == 1) {
      cin >> x1 >> y1 >> c;
      update(a[x1][y1], x1, y1, -1);
      a[x1][y1] = c;
      update(a[x1][y1], x1, y1, 1);
    } else {
      cin >> x1 >> x2 >> y1 >> y2 >> c;
      int s22 = query(c, x2, y2);
      int s12 = query(c, x1 - 1, y2);
      int s21 = query(c, x2, y1 - 1);
      int s11 = query(c, x1 - 1, y1 - 1);
      cout << s22 - s12 - s21 + s11 << endl;
    }
  }
  return 0;
}